package com.helei.strategy;

import com.helei.entity.root.MessageGroup;
import com.helei.service.ApiService;
import com.helei.service.Impl.QQProperties;
import com.helei.utils.MyUtils;
import com.helei.utils.SensitiveWordCheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
@Slf4j
public class CalcPriceService {
    @Autowired
    ApiService apiService;
    @Autowired
    private SensitiveWordCheckUtils sensitiveWordCheckUtils;
    @Autowired
    QQProperties qqProperties;

    public String group(MessageGroup message) {
        boolean memberInfo = apiService.get_group_member_info(message.getGroupId());
        log.info("收到客户端信息:{}", message);
        log.info("收到群消息:群号:{},--用户昵称:{},--用户群昵称:{},--用户QQ号:{},--消息:{}", message.getGroupId(), message.getSender().getNickname(), message.getSender().getCard(), message.getSender().getUserId(), message.getMessage());
        Set<String> sens = sensitiveWordCheckUtils.getSensitiveWordMaxMatch(message.getMessage());
        if ((!message.getMessage().contains("CQ:image")) && memberInfo && !sens.isEmpty()) {
            log.info("敏感词：：{}", sens);
            boolean status = apiService.deleteMsg(message.getMessageId());
            log.info(status ? "撤回成功" : "撤回失败");
            //send_msg(null, "敏感词:" + new Gson().toJson(sens) + "消息撤回成功", "group", message.getGroupId());
        }
        //策略
        if (message.getMessage().contains("撤回")) {
            int index = 0;
            try {
                String substring = MyUtils.number(message.getMessage());
                index = Integer.parseInt(substring);
            } catch (Exception e) {
                log.info("撤回多次消息命令有误:{}", message.getMessage());
            }
            int i = 1;
            index = Math.min(index, 200);
            apiService.deleteMsgAll(message.getGroupId(), String.valueOf(Integer.parseInt(message.getMessageSeq()) - 1), i, index);
            log.info("撤回成功:{}", message.getMessage());
        }
        if (message.getMessage().equals("[CQ:at,qq=" + qqProperties.getQq() + "] 命令")) {
            apiService.send_msg(null, "群聊:\n命令1---撤回消息发送:撤回n条", "group", message.getGroupId());
        }
        return "成功";
    }
    public String individual(MessageGroup message) {

        String message1 = message.getMessage();

           log.info("收到个人消息:{}", message1);
      /*  String[] split = message1.split("__");
        String[] split1 = split[1].split("\\|");
        for (int i = 0; i < split1.length; i++) {
            System.out.println(split[0]+":"+split1[i]);
            apiService.send_msg( split1[i],split[0], "private", null);
        }*/
        return "成功";
    }
}
